package olga.gorbatiuk.record;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public final class Record {
    private final Date time;
    private final Importance importance;
    private final String messageSource;
    private final String message;

    public Record(Date time, Importance importance, String messageSource,
	    String message) {
	this.time = new Date(time.getTime());
	this.importance = importance;
	this.messageSource = messageSource;
	this.message = message;

    }

    // TODO дать посмотреть Сереже. Непонятно описано требование - непонянто
    // написан конструктор.
    public Record(String errorMessage) {
	errorMessage.trim();
	String[] arr = errorMessage.split("\\s+");

	StringBuilder sb = new StringBuilder();
	sb.append(arr[0]).append(" ").append(arr[1]);
	String date = sb.toString();

	SimpleDateFormat dateFormat = new SimpleDateFormat(
		"yyyy-mm-dd hh:mm:ss");
	Date time = new Date();
	try {
	    time = dateFormat.parse(date);
	} catch (ParseException e) {
	    e.printStackTrace();
	}

	sb = new StringBuilder();
	for (int i = 4; i < arr.length; i++) {
	    sb.append(arr[i]).append(" ");
	}
	Importance importance = Importance.valueOf(arr[2]);
	this.time = time;
	this.importance = importance;
	this.messageSource = arr[3];
	this.message = sb.toString();
    }

    public Date getTime() {
	return new Date(this.time.getTime());
    }

    public Importance getImportance() {
	return importance;
    }

    public String getMessageSource() {
	return messageSource;
    }

    public String getMessage() {
	return message;
    }

    @Override
    public int hashCode() {
	final int prime = 31;
	int result = 1;
	result = prime * result
		+ ((importance == null) ? 0 : importance.hashCode());
	result = prime * result + ((message == null) ? 0 : message.hashCode());
	result = prime * result
		+ ((messageSource == null) ? 0 : messageSource.hashCode());
	result = prime * result + ((time == null) ? 0 : time.hashCode());
	return result;
    }

    @Override
    public boolean equals(Object obj) {
	if (this == obj)
	    return true;
	if (obj == null)
	    return false;
	if (getClass() != obj.getClass())
	    return false;
	Record other = (Record) obj;
	if (importance != other.importance)
	    return false;
	if (message == null) {
	    if (other.message != null)
		return false;
	} else if (!message.equals(other.message))
	    return false;
	if (messageSource == null) {
	    if (other.messageSource != null)
		return false;
	} else if (!messageSource.equals(other.messageSource))
	    return false;
	if (time == null) {
	    if (other.time != null)
		return false;
	} else if (!time.equals(other.time))
	    return false;
	return true;
    }

    @Override
    public String toString() {
	SimpleDateFormat dateFormat = new SimpleDateFormat(
		"yyyy-mm-dd hh:mm:ss");
	return dateFormat.format(time) + " " + importance + " " + messageSource;
    }
}
